package com.heima.comment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.comment.service.CommentService;
import com.heima.common.aliyun.GreenTextScan;
import com.heima.common.exception.CustomException;
import com.heima.model.comment.dtos.CommentSaveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.utils.common.UserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private GreenTextScan greenTextScan;

    @Autowired
    private IWemediaClient wemediaClient;

    /**
     * 保存评论
     *
     * @param commentSaveDto
     * @return
     */
    @Override
    public ResponseResult save(CommentSaveDto commentSaveDto) throws Exception {
        //1.判断登录状态
        ApUser user = UserThreadLocalUtil.getUser();
        if (user == null || user.getId() == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        //2.长度不能超过140
        if (commentSaveDto.getContent().length() <= 140) {
            //3.评论文本垃圾检测
            ResponseResult responseResult = wemediaClient.findAll();
            Object data = responseResult.getData();
            String jsonString = JSON.toJSONString(data);
            List<WmSensitive> wmSensitives = JSONArray.parseArray(jsonString, WmSensitive.class);
            List<String> sensitives = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
            SensitiveWordUtil.initMap(sensitives);
            Map<String, Integer> map = SensitiveWordUtil.matchWords(commentSaveDto.getContent());
            if (map.size() > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评论含有不当内容");
            }
            Map greeTextScan = greenTextScan.greeTextScan(commentSaveDto.getContent());
            if (greeTextScan != null) {
                if ("block".equals(greeTextScan.get("suggestion"))) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评论含有不当内容" );
                }
                if ("review".equals(greeTextScan.get("suggestion"))) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评论含有不当内容");
                }
            }
        }
        //4.保存评论到mongoDB
        CommentSaveDto save = mongoTemplate.save(commentSaveDto);
        return ResponseResult.okResult(save);
    }
}
